package com.jshongli.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.util.Log;
import android.widget.ImageView;

import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.bumptech.glide.Priority;
import com.cktim.camera2library.Camera2Config;
import com.lzy.ninegrid.NineGridView;

import java.net.URLDecoder;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import com.zhy.http.okhttp.OkHttpUtils;
import okhttp3.logging.HttpLoggingInterceptor;

public class MyApplication extends MultiDexApplication {

    private static MyApplication mInstance;

    public static MyApplication getInstance() {
        return mInstance;
    }


//     NineGridView通过接口实现图片的加载
    private class GlideImageLoader implements NineGridView.ImageLoader {
        @Override
        public void onDisplayImage(Context context, ImageView imageView, String url) {
            GlideApp.with(context)
                    .load(url)
                    .fitCenter()
                    .placeholder(R.mipmap.ic_launcher)//
                    .error(R.mipmap.ic_launcher)//
                    .priority(Priority.HIGH)
                    .into(imageView);
        }

        @Override
        public Bitmap getCacheImage(String url) {
            return null;
        }
    }


    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        initBaiDuMap();
        initCamera2();
        initOkHttpUtils();
        // 九宫格图片点击预览
        NineGridView.setImageLoader(new GlideImageLoader());


    }


    //region
    private static void initOkHttpUtils() {

        // OkHttpClient 配置
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        // 如果是DEBUG模式添加HttpLogging拦截器
        if (BuildConfig.DEBUG) {
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                @Override
                public void log(String message) {
                    try {
                        Log.d("HttpLogging", URLDecoder.decode(message, "utf-8"));
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.d("HttpLogging_Error", message);
                    }
                }
            });
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

            builder.addInterceptor(loggingInterceptor);
        }

        // OkHttpClient 配置
        builder.connectTimeout(30000L, TimeUnit.MILLISECONDS)
                .readTimeout(30000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();

        OkHttpClient client = builder.build();
        OkHttpUtils.initClient(client);
    }
    //endregion


    private void initBaiDuMap(){
        //在使用SDK各组件之前初始化context信息，传入ApplicationContext
        SDKInitializer.initialize(this);
        //自4.3.0起，百度地图SDK所有接口均支持百度坐标和国测局坐标，用此方法设置您使用的坐标类型.
        //包括BD09LL和GCJ02两种坐标，默认是BD09LL坐标。
        SDKInitializer.setCoordType(CoordType.GCJ02);
    }




    //region
    public void initCamera2() {
        // 配置Camera2相关参数，
        Camera2Config.RECORD_MAX_TIME = 10;
        Camera2Config.RECORD_MIN_TIME = 2;
        Camera2Config.RECORD_PROGRESS_VIEW_COLOR = R.color.colorAccent;
        Camera2Config.PREVIEW_MAX_HEIGHT = 1300;
        Camera2Config.PATH_SAVE_VIDEO = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ZHZMVideo/";
        Camera2Config.PATH_SAVE_PIC = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ZHZMVideo/photo/";
        // 是否需要拍照功能，设置为false
        Camera2Config.ENABLE_CAPTURE = false;
    }
    //endregion



    @Override
    protected void attachBaseContext(Context base) {
        MultiDex.install(this);
        super.attachBaseContext(base);
    }

}


